from os import chdir, mkdir, system ,getenv
from os import path as opath
from sys import path
from random import randint
from socket import socket

ip = '127.0.0.1'

appdata = getenv('appdata')

ip_lst = []

def port_can_use(port):
    if port in ip_lst :
        return False
    sk = socket()
    try :
        sk.bind(('',port))
    except Exception as e :
        print(e)
        return False
    ip_lst.append(port)
    return True
    

i = 0
# print(port_can_use(445))
if not opath.exists(appdata + '\\OjNetCompileClient' ) :
        mkdir(appdata + '\\OjNetCompileClient' )  
# if not opath.exists(appdata + '\\OjNetCompileClient\\judgemachine') :
#         mkdir(appdata + '\\OjNetCompileClient\\judgemachine' )  
for i in range(34) :
    if not opath.exists(appdata + '\\OjNetCompileClient\\judgemachine'+str(int(i))) :
        mkdir(appdata + '\\OjNetCompileClient\\judgemachine'+str(int(i)))  
    system('copy '+path[0]+'\\JUDGE\\ %APPDATA%\\OjNetCompileClient\\judgemachine'+str(int(i))) 
    f = open(appdata + '\\OjNetCompileClient\\judgemachine'+str(int(i))+'\\compile.ini' , 'w')
    f.write('[ip]\nserverip = ')
    f.write(ip)
    f.write('\n[myip]\nmyport = ')
    pt = randint(0,65530)
    while not port_can_use(pt) :
        pt = randint(0,65530)
    f.write(str(pt))
    f.close()